<?php
use think\Session;
use think\Db;
$userType=Db::name('love_admin_user')->where(['id'=>Session::get('yoshop_admin')['user']['id']])->value('type');

?>
<div class="row">
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
        <div class="widget am-cf">
            <div class="widget-head am-cf">
                <div class="widget-title am-cf">客户列表</div>
            </div>
            <div class="widget-body am-fr">
                <div class="page_toolbar am-margin-bottom-xl am-cf">
                    <form id="form-search" class="toolbar-form" action="">
                        <input type="hidden" name="s" value="/<?= $request->pathinfo() ?>">
                        <?php if ($userType==1): ?>
                            <div class="am-u-sm-12 am-u-md-3">
                                <div class="am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <?php if (checkPrivilege('store.store/goodsExport')): ?>
                                                <a class="j-export am-btn am-btn-success am-radius"
                                                   href="javascript:void(0);">
                                                    <i class="iconfont icon-daochu am-margin-right-xs"></i>客户数据导出
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="am fr">
                            <div class="am-form-group am-fl">
                                <?php $gender = $request->get('datatype'); ?>
                                <select name="datatype"
                                        data-am-selected="{btnSize: 'sm', placeholder: '选择客户类型'}">
                                    <option value=""></option>
                                    <option value="1"
                                        <?= $gender === '1' ? 'selected' : '' ?>>买
                                    </option>
                                    <option value="2"
                                        <?= $gender === '2' ? 'selected' : '' ?>>卖
                                    </option>
                                    <option value="3"
                                        <?= $gender === '3' ? 'selected' : '' ?>>出租
                                    </option>
                                    <option value="4"
                                        <?= $gender === '4' ? 'selected' : '' ?>>求租
                                    </option>
                                </select>
                            </div>
                            <div class="am-form-group am-fl">
                                <div class="am-input-group am-input-group-sm tpl-form-border-form">
                                    <input type="text" class="am-form-field" name="search"
                                           placeholder="客户名称"
                                           value="<?= $request->get('search') ?>">
                                    <div class="am-input-group-btn">
                                        <button class="am-btn am-btn-default am-icon-search" type="submit"></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="am-u-sm-12">
                    <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                        <thead>
                        <tr>
                            <th>客户ID</th>
                            <th>客户名称</th>
                            <th>录入人</th>
                            <th>录入人类型</th>
                            <th>录入方式</th>
                            <th>当前跟进人</th>
                            <th>客户类型</th>
                            <th>客户意向</th>
                            <th>客户状态</th>
                            <th>录入时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php if (!$list->isEmpty()): foreach ($list as $item): ?>
                            <tr>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= $item['id'] ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= $item['name'] ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= upname($item['inputuserid']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= types($item['inputusertype']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= getluruType($item['iszilu']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= upname($item['floweuserid']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= getcutype($item['datatype']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= getcuflag($item['flagid']) ?></p>
                                </td>
                                <td class="am-text-middle">
                                    <p class="item-title"><?= getcustatus($item['status']) ?></p>
                                </td>
                                <td class="am-text-middle"><?= gettimes($item['create_time']) ?></td>

                                <td class="am-text-middle">
                                    <div class="tpl-table-black-operation">
                                        <a href="<?= url('store.store/getinfo',['id' => $item['id']]) ?>" target="_blank">
                                            <i class="am-icon-binoculars"></i> 查看详情
                                        </a>

                                        <?php if ($userType==3): ?>
                                            <?php if ($item['status']==4): ?>
                                                <a onclick="shenhe(<?=$item['id'] ?>)">
                                                    <i class="am-icon-pause-circle-o"></i> 预失效审核
                                                </a>
                                            <?php endif; ?>
                                            <?php if (checkes($item['id'])): ?>
                                                <a onclick="changecus(<?=$item['id'] ?>)">
                                                    <i class="am-icon-wrench"></i> 转客
                                                </a>
                                            <?php endif; ?>
                                            <?php if (chongzhi($item['id'])): ?>
                                                <a onclick="cz(<?=$item['id'] ?>)"
                                                   class="item-delete tpl-table-black-operation-del"
                                                   >
                                                    <i class="am-icon-wrench j-delete"></i> 重置客户
                                                </a>
                                            <?php endif; ?>

                                        <?php endif; ?>

                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; else: ?>
                            <tr>
                                <td colspan="11" class="am-text-center">暂无记录</td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="am-u-lg-12 am-cf">
                    <div class="am-fr"><?= $list->render() ?> </div>
                    <div class="am-fr pagination-total am-margin-right">
                        <div class="am-vertical-align-middle">总记录：<?= $list->total() ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    //重置客户
    function cz(id){
        $.ajax({
            url:"<?php echo url('store.store/recovery'); ?>",
            data:{id:id},
            type:'post',
            async: false,
            success:function(res) {
                if(res.code == 1) {
                    layer.alert(res.msg, function(index){
                        location.href = "<?php echo url('store.store/index'); ?>";
                    })
                }else{
                    layer.msg(res.msg);
                }
            }
        })
    }
    //审核
    function shenhe(id){
        layer.msg('请选择操作', {
            time: 0,
            shade : 0.5,
            shadeClose: true, //点击遮罩关闭层
            btnAlign: 'c',
            btn: ['审核通过', '驳回申请','重新分配'],
            yes: function(index, layero) {
                dosome(id,1)
            },
            btn2:function(index, layero) {
                dosome(id,2)
            },
            btn3:function(index, layero) {
                dosome(id,3)
            }
        });
    }
    function dosome(id,type){
        $.ajax({
            url:"<?php echo url('store.store/shenhe'); ?>",
            data:{id:id,status:type},
            type:'post',
            async: false,
            success:function(res) {
                if(res.code == 1) {
                    layer.alert(res.msg, function(index){
                        location.href = "<?php echo url('store.store/index'); ?>";
                    })
                }else{
                    layer.msg(res.msg);
                }
            }
        })
    }
    //转客操作
    function changecus(id){
        //获取当前登录人的所有置业顾问和置业经理信息
        $.ajax({
            url:"<?php echo url('store.store/getuser'); ?>",
            data:{},
            type:'post',
            async: false,
            success:function(res) {
                if(res.code == 1) {
                    var str='';
                    res.data.forEach(function(element) {
                        str+='<option value='+element.id+'>'+element.name+'</option>';
                    });
                    layer.open({
                        title: '选择接收人'
                        , btn: ['确定']
                        , content: '<div class="layui-form-item"><div class="layui-input-block"><select name="range" id="range" lay-filter="range">'+str+'</select></div></div>',
                        yes: function (index, layero) {
                            var userid=$('#range').val()
                            $.ajax({
                                url:"<?php echo url('store.store/change'); ?>",
                                data:{userid:userid,cusid:id},
                                type:'post',
                                async: false,
                                success:function(res1) {
                                    layer.alert(res1.msg, function(index){
                                        location.href = "<?php echo url('store.store/index'); ?>";
                                    })
                                }
                            })
                        }
                    });
                }else{
                    layer.msg(res.msg);
                }
            }
        })
    }
    $(function () {
        /**
         * 订单导出
         */
        $('.j-export').click(function () {
            var data = {};
            var formData = $('#form-search').serializeArray();
            $.each(formData, function () {
                this.name !== 's' && (data[this.name] = this.value);
            });
            window.location = "<?= url('store.store/goodsExport') ?>" + '&' + $.urlEncode(data);
        });
    });
</script>

